package com.chuangke.common.data.builder;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@SuppressWarnings("rawtypes")
public class DataBuilder {

	private Collection sourceDatalist;
	private Map<String, Map> metaDataMap;
	private Map<String, IDataBuilder> dataBuilderMap;
	private Map<String, List<DataBuildConfig>> dataBuildConfigMap;

	public static DataBuilder of(Collection list) {
		DataBuilder dataBuilder = new DataBuilder();
		dataBuilder.sourceDatalist = list == null ? new ArrayList<>() : list;
		dataBuilder.metaDataMap = new HashMap<>();
		dataBuilder.dataBuilderMap = new HashMap<>();
		dataBuilder.dataBuildConfigMap = new HashMap<>();
		return dataBuilder;
	}

	public DataBuilder appendBuilder(IDataBuilder dataBuilder, DataBuildConfig... dataBuildConfig) {
		metaDataMap.put(dataBuilder.getName(), dataBuilder.getMetaData());
		dataBuilderMap.put(dataBuilder.getName(), dataBuilder);
		dataBuildConfigMap.put(dataBuilder.getName(), Arrays.asList(dataBuildConfig));
		return this;
	}

	@SuppressWarnings("unchecked")
	public void build() {
		sourceDatalist.forEach(obj -> {
			for (String key : dataBuilderMap.keySet()) {
				dataBuilderMap.get(key).build(obj, metaDataMap.get(key), dataBuildConfigMap.get(key));
			}
		});
	}
}
